package com.whut.monitor.controller;

import com.whut.monitor.vo.*;
import com.whut.monitor.model.TimeMachiningEntity;
import com.whut.monitor.service.ITimeMachiningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
//import java.util.Date;
import java.util.List;
import java.sql.Date;

/**
 * Created by CFlower on 2017/11/7.
 */
@Controller
@RequestMapping("/timeMachining")
public class TimeMachiningController {
    @Autowired
    private ITimeMachiningService timeMachiningService;

    @RequestMapping("/manager")
    public String manager() {
        return "/mintor/sensorConfiguration/timeMachining";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(TimeMachiningVo timeMachiningVo, PageFilter pageFilter) {
        Grid grid = new Grid();
        grid.setRows(timeMachiningService.dataGrid(timeMachiningVo, pageFilter));
        grid.setTotal(timeMachiningService.count(timeMachiningVo, pageFilter));
        return grid;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMsg add(TimeMachiningVo timeMachiningVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            timeMachiningService.add(timeMachiningVo);
            j.setSuccess(true);
            j.setMsg("添加成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMsg edit(TimeMachiningVo timeMachiningVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            timeMachiningService.edit(timeMachiningVo);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMsg delete(Integer id) {
        ResponseMsg j = new ResponseMsg();
        try {
            timeMachiningService.delete(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return timeMachiningService.tree();
    }

    @RequestMapping("/getMachiningByEquip")
    @ResponseBody
    public List<ComboVo> getMachiningByEquip(Integer equipmentId) {
        List<ComboVo> list = new ArrayList<>();
        List<TimeMachiningEntity> entity = timeMachiningService.getMachiningByEquip(equipmentId);
        for (TimeMachiningEntity timeMachiningEntity : entity) {
            ComboVo comboVo = new ComboVo();
            comboVo.setValue(timeMachiningEntity.getMachiningEntity().getId());
            comboVo.setText(timeMachiningEntity.getMachiningEntity().getMachiningName());
            comboVo.setNum(timeMachiningEntity.getMachiningEntity().getMachiningName());
            list.add(comboVo);
        }
        return list;
    }

    @RequestMapping("/getDateByEquipMachine")
    @ResponseBody
    public List<ComboVo> getDateByEquipMachine(Integer equipmentId, Integer machiningId) {
        List<ComboVo> list = new ArrayList<>();
        List<TimeMachiningEntity> entity = timeMachiningService.getDateByEquipMachine(equipmentId, machiningId);
        for (TimeMachiningEntity timeMachiningEntity : entity) {
            ComboVo comboVo = new ComboVo();
            comboVo.setValue(timeMachiningEntity.getId());
            comboVo.setText(timeMachiningEntity.getMachiningDate().toString());
            comboVo.setNum(timeMachiningEntity.getMachiningDate().toString());
            list.add(comboVo);
        }
        return list;
    }

    @RequestMapping("/getProcessByEquipMachDate")
    @ResponseBody
    public List<ComboVo> getProcessByEquipMachDate(Integer equipmentId, Integer machiningId, String machiningDate) {
        List<ComboVo> list = new ArrayList<>();
        // 将字符串转变为日期格式
        Date datte = Date.valueOf(machiningDate);
        List<TimeMachiningEntity> entity = timeMachiningService.getProcessByEquipMachDate(equipmentId, machiningId, datte);
        for (TimeMachiningEntity timeMachiningEntity : entity) {
            ComboVo comboVo = new ComboVo();
            comboVo.setValue(timeMachiningEntity.getId());
            // 格式化时间输出
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            String StartTime = format.format(timeMachiningEntity.getStartTime());
            String EndTime = format.format(timeMachiningEntity.getEndTime());

            comboVo.setText(StartTime + "-" + EndTime);
            comboVo.setNum(StartTime + "-" + EndTime);
            list.add(comboVo);
        }
        return list;
    }

    @RequestMapping("/getAllData")
    @ResponseBody
    public List<ExperimentVo> getAllData(Integer equipmentId) {
        List<ExperimentVo> list = new ArrayList<>();
        List<TimeMachiningEntity> entity = timeMachiningService.getAllData(equipmentId);
        for (TimeMachiningEntity timeMachiningEntity : entity) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            String startTime = format.format(timeMachiningEntity.getStartTime());
            String endTime = format.format(timeMachiningEntity.getEndTime());

            ExperimentVo experimentVo = new ExperimentVo();
            experimentVo.setWork(timeMachiningEntity.getMachiningEntity().getMachiningName());
            experimentVo.setDate(timeMachiningEntity.getMachiningDate().toString().substring(0, 10));
            experimentVo.setTime(startTime + "-" + endTime);
            list.add(experimentVo);
        }
        return list;
    }
}
